import {
  getPromotion,
  cilckRecord,
  listECPlatform,
  listGoods,
  listActivities
} from '../../utils/api'


Page({

  /**
   * 页面的初始数据
   */
  data: {
    pdds: [],
    skus: [],
    show: false,
    platforms: [],
    page: 1,
    isOver: false,
    pageContainer: false,
    activityIndex: 0,
    keyword: '',
    loading: true,
    platform: 3
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.listActivities()
    this.listECPlatform()
  },
  /**
   * 查询拼多多活动
   */
  listActivities(){
    listActivities({
      platform: this.data.platform
    }).then(res=>{
      this.setData({
        pdds: res.data,
        loading: false
      })
    })
  },
  // 上拉触底函数
  onReachBottom: function () {
    if (!this.data.isOver) {
      setTimeout(() => {
        this.listGoods()
      }, 500);
    }
  },
  // 多多频道
  pdd(e) {
    wx.showLoading({
      title: '加载中'
    })
    getPromotion({
      platform: this.data.platform,
      actId: this.data.pdds[e.currentTarget.dataset.index].actId
    }).then(res=>{
      wx.hideLoading()
      wx.navigateToMiniProgram({
        appId: res.data.appId,
        path: res.data.pagePath,
        success: res => {
          cilckRecord({
            clickType: this.data.pdds[e.currentTarget.dataset.index].actId
          })
        }
      })
    })
  },
  /**
   * 去美团页
   */
  goToMeituan() {
    wx.switchTab({
      url: '../mt/mt',
    })
  },
  /**
   * 去饿了么页
   */
  goToElm() {
    wx.switchTab({
      url: '../elm/elm',
    })
  },
  onLoadOfficialAccount() {
    setTimeout(() => {
      this.setData({
        pageContainer: true
      })
    }, 1000);
  },
  /**
   * 查询电商平台
   */
  listECPlatform() {
    listECPlatform().then(res => {
      this.setData({
        platforms: res.data
      })
      this.listGoods()
    })
  },
  /**
   * 查询电商商品
   */
  listGoods() {
    if (this.data.isOver) {
      return false
    }
    this.setData({
      show: true
    })
    listGoods({
      page: this.data.page++,
      pageSize: 20,
      source: this.data.platforms[this.data.activityIndex].source,
      type: 1,
      keyword: this.data.keyword,
      sort: 1,
      isSearchByKeyword: this.data.keyword === '' ? false : true,
      subShareRate: wx.getStorageSync('rate')
    }).then(res => {
      if (res.data) {
        this.setData({
          skus: this.data.skus.concat(res.data)
        })
      } else {
        this.setData({
          show: false,
          isOver: true
        })
      }
    })
  },
  /**
   * 点击选择平台
   */
  onClickPlatform(e) {
    this.setData({
      activityIndex: e.currentTarget.dataset.index,
      skus: [],
      page: 1,
      isOver: false
    })
    this.listGoods()
  },
  /**
   * 点击输入关键字的时候跳转到goods
   */
  onClickSearch() {
    if (this.data.keyword === '') {
      return false
    }
    this.setData({
      skus: [],
      page: 1
    })
    this.listGoods()
  },
  /**
   * 领券
   */
  skuBuy(e) {
    wx.navigateTo({
      url: '../goodsDetail/goodsDetail?goodsId=' + this.data.skus[e.currentTarget.dataset.index].goodsId + '&source=' + this.data.platforms[this.data.activityIndex].source,
    })
  }
})